# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


class Solution:
    def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
        if m == n:
            return head
        head, head.next, index = ListNode(None), head, 0
        now_note = head
        while index <= n:
            if index == m - 1:
                fanzhuantou = now_note
                mid_tou = now_note.next
                mid_now = mid_tou
            if index > m - 1:
                now_note.next, now_note, mid_now = mid_now, now_note.next, now_note
                index += 1
                continue
            now_note = now_note.next
            index += 1
        mid_tou.next = now_note
        fanzhuantou.next = mid_now
        return head.next
